package com.javaweb.common;

import com.javaweb.entity.Product;
import com.javaweb.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Slf4j
@Aspect
@Component
public class ShoppingCartAspect {
    @Autowired
    private ProductService productService;

    @Pointcut("execution(* com.javaweb.service.ShoppingCartService.add(..))")
    public void add(){}
    @Pointcut("execution(* com.javaweb.service.ShoppingCartService.remove(..))")
    public void remove(){}
    @Pointcut("execution(* com.javaweb.service.ShoppingCartService.listAll(..))")
    public void listAll(){}

    @Before("add()")
    public void checkStock(JoinPoint joinPoint) {
        // 在添加商品到购物车之前，检查商品库存是否充足的逻辑
        // 可以使用productService.findById()方法来获取商品信息，并进行库存的检查
        log.info("Check if the product stock is sufficient.");
    }

    @AfterReturning("remove()")
    public void logRemoveOperation(JoinPoint joinPoint) {
        // 在删除商品时，记录删除操作的日志逻辑

         log.info("Product has been deleted.");

    }

    @AfterReturning(pointcut = "execution(* com.javaweb.service.ShoppingCartService.listAll())", returning = "result")
    public void logListAllOperation(JoinPoint joinPoint, List<Product> result) {
        // 在获取购物车商品列表时，记录获取操作的日志逻辑
        log.info("Perform operations on the list.");

    }
}
